Ext.ns("googlemap");

googlemap.statics = {};
googlemap.statics.defaultOpt = eval(getCategoryInfo('json' , '' , '' , '' , { 'category_gubun' : 'c001' }));

googlemap.greenIcon = new google.maps.MarkerImage(
	 "http://labs.google.com/ridefinder/images/mm_20_green.png",
	   new google.maps.Size(12, 20),
	   new google.maps.Point(0, 0),
	   new google.maps.Point(6, 20)
);

googlemap.makeMap = function(map_div , latlng , xmldata) {

	/*
	$(map_div).css({
		width: main.winWidth ,
		height: main.winHeight
	});
	*/
	$(map_div).css({
		width: '100%' ,
		height: '100%'
	});
	var gmap = new google.maps.Map(
		map_div , {
			zoom:16,
			center: latlng ,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}		
	);
	
	var marker = new google.maps.Marker({
		icon: googlemap.greenIcon,
		position: latlng,
		map: gmap,
		title: '현재 위치'
	});
	googlemap.statics.markers = new Array();
	var cursor , curLat , curLng , curTitle , curCate1 , curSeq;
	$(xmldata).find('item').each(function(){
		cursor = $(this);
		curLat = cursor.find('lat').text();
		curLng = cursor.find('lng').text();
		curTitle = cursor.find('name').text();
		curCate1 = cursor.find('category1').text();
		curSeq = cursor.find('info_seq').text();
		if(curLat && curLng) {
			var marker = new google.maps.Marker({
				position: new google.maps.LatLng(curLat , curLng) ,
				map: gmap,
				title: curTitle
			});
			marker.cate1 = curCate1;
			googlemap.statics.markers.push( marker );
			var contentString = "<div style='font-size:12px; width:100%; height:100%; background:#eeeeee;' onclick='map.panel.make.carcenter( "+curSeq+" )' >"
								+ "<strong>["+curTitle+"]</strong>"
								+ "<br />" + cursor.find('addr').text()
								+ "<br />" + cursor.find('phone').text()
								+"</div>";
			var infowindow = new google.maps.InfoWindow({
		        content: contentString,
		        maxWidth: 200
		    });
			
		//	$('#carcInfoIWindow' + curSeq).click(function(){ 
		//		map.panel.make.carcenter( cursor );
		//	});
			
			google.maps.event.addListener(marker , 'click' , function(){
			//	googlemap.cursor = cursor;
				Ext.Msg.alert("알림" , 1);
			//	infowindow.open(gmap , marker);
			});
			
		}
	});
	
	return gmap;

};

googlemap.Control = function(controlDiv , map) {
	// Set CSS styles for the DIV containing the control  
	// Setting padding to 5 px will offset the control  
	// from the edge of the map  
	controlDiv.style.padding = '5px';  
	controlDiv.style.margin = '15px';  
	// Set CSS for the control border  
	
	controlDiv.style.backgroundColor = 'white';  
	controlDiv.style.borderStyle = 'solid';  
	controlDiv.style.borderWidth = '2px';  
	controlDiv.style.cursor = 'pointer';  
	controlDiv.style.textAlign = 'center';  
	controlDiv.style.fontFamily = 'Arial,sans-serif';  
	controlDiv.style.fontSize = '12px';  controlDiv.style.paddingLeft = '4px';  
	controlDiv.style.paddingRight = '4px';  controlDiv.innerHTML = '전체';  
	controlDiv.style.width = '80px';
	/*
	var controlUI = document.createElement('DIV');  
	controlUI.style.backgroundColor = 'white';  
	controlUI.style.borderStyle = 'solid';  
	controlUI.style.borderWidth = '2px';  
	controlUI.style.cursor = 'pointer';  
	controlUI.style.textAlign = 'center';  
	controlDiv.appendChild(controlUI);  
	// Set CSS for the control interior  
	var controlText = document.createElement('DIV');  
	controlText.style.fontFamily = 'Arial,sans-serif';  
	controlText.style.fontSize = '12px';  controlText.style.paddingLeft = '4px';  
	controlText.style.paddingRight = '4px';  controlText.innerHTML = 'Home';  
	controlUI.appendChild(controlText);  
	*/
	
	$(controlDiv).click(function(){
		var curVal = $(this).attr('cval');
		curVal = (++curVal) % (googlemap.statics.defaultOpt.length+1);
		$(this).attr('cval' , curVal);
		
		var text = curVal == 0 ? '전체' : googlemap.statics.defaultOpt[curVal-1].category_name;
		
		var cate1Value = curVal == 0 ? '0' : googlemap.statics.defaultOpt[curVal-1].category_value;
		
		$(this).text( text );

		for( i in googlemap.statics.markers ) {
			if(googlemap.statics.markers[i].setMap) {
				if( cate1Value != 0 && googlemap.statics.markers[i].cate1 != cate1Value) {
					googlemap.statics.markers[i].setMap(null);
				}
				else {
					googlemap.statics.markers[i].setMap(map);
				}
			}
		}
	});
	
	// Setup the click event listeners: simply set the map to Chicago  
};